﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using SignalrService.Models;
using SignalrService.SignalrService;

namespace SignalrService.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IHubContext<CommonHub> _hubContext;
        private readonly IOnlineClientManager _onlineClientManager;

        public HomeController(ILogger<HomeController> logger, IHubContext<CommonHub> hubContext, IOnlineClientManager onlineClientManager)
        {
            _logger = logger;
            _hubContext = hubContext;
            _onlineClientManager = onlineClientManager;
        }

        public async Task<IActionResult> Index(string messge)
        {
           
          
            if (!string.IsNullOrEmpty(messge))
            {
                await _hubContext.Clients.All.SendAsync("ReceiveMessage", "链接id", $"{messge}:{DateTime.Now}");
            }

            List<string> list = new List<string>();
            ViewBag.Sid = _onlineClientManager.GetAllClients().Count;



            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
